package org.example4;

import java.lang.reflect.Method;

public class MyProxyFactory implements JavaInvocationHandler {

    private Person target;

    public Object getInstance(Person target){
        this.target=target;
        Class<?> clazz=target.getClass();
        return JavaProxy.newProxyInstance(new JavaClassLoader(),clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object returnVal = method.invoke(target, args);
        after();
        return returnVal;
    }

    public void before() {
        System.out.println("自定义动态代理开始了");
    }

    public void after() {
        System.out.println("自定义动态代理结束了");
    }
}